package org.batatasframework.values;

import java.util.Date;
import java.util.List;

import org.springframework.util.Assert;

public abstract class AbstractValueReader implements ValueReader{

	@Override
	public String getString(String name) {
		return getValue(name).getString();
	}

	@Override
	public String getString(String name, String defaultValue) {
		String value = getValue(name).getString();
		return null != value ? value : defaultValue;
	}
	
	@Override
	public String notNullString(String name, String message) {
		String value = getValue(name).getString();
		Assert.notNull(value, message);
		return value;
	}
	
	@Override
	public boolean is(String name, Object target) {
		return getValue(name).is(target);
	}

	@Override
	public Integer getInteger(String name) {
		return getValue(name).getInteger();
	}

	@Override
	public Long getLong(String name) {
		return getValue(name).getLong();
	}

	@Override
	public Boolean getBoolean(String name) {
		return getValue(name).getBoolean();
	}

	@Override
	public int getInteger(String name, int defaultValue) {
		Integer value = getValue(name).getInteger();
		return null != value ? value : defaultValue;
	}
	
	@Override
	public int notNullInteger(String name, String message) {
		Integer value = getValue(name).getInteger();
		Assert.notNull(value, message);
		return value;
	}

	@Override
	public long getLong(String name, long defaultValue) {
		Long value = getValue(name).getLong();
		return null != value ? value : defaultValue;
	}
	
	@Override
	public long notNullLong(String name, String message) {
		Long value = getValue(name).getLong();
		Assert.notNull(value, message);
		return value;
	}

	@Override
	public boolean getBoolean(String name, boolean defaultValue) {
		Boolean value = getValue(name).getBoolean();
		return null != value ? value : defaultValue;
	}
	
	@Override
	public boolean notNullBoolean(String name, String message) {
		Boolean value = getValue(name).getBoolean();
		Assert.notNull(value, message);
		return value;
	}

	@Override
	public Double getDouble(String name) {
		return getValue(name).getDouble();
	}

	@Override
	public double getDouble(String name, double defaultValue) {
		Double value = getValue(name).getDouble();
		return null != value ? value : defaultValue;
	}
	
	@Override
	public double notNullDouble(String name, String message) {
		Double value = getValue(name).getDouble();
		Assert.notNull(value, message);
		return value;
	}

	@Override
	public <T> List<T> asList(String name) {
		return getValue(name).asList();
	}

	@Override
	public <E extends Enum<E>> E toEnum(String name, Class<E> clazz) {
		return getValue(name).toEnum(clazz);
	}

	@Override
	public <E extends Enum<E>> E toEnum(String name, Class<E> clazz, E defaultValue) {
		E value = getValue(name).toEnum(clazz);
		return null != value ? value : defaultValue;
	}
	
	@Override
	public <E extends Enum<E>> E notNullEnum(String name, Class<E> clazz, String message) {
		E value = getValue(name).toEnum(clazz);
		Assert.notNull(value, message);
		return value;
	}

	@Override
	public <T> T getObject(String name, Class<T> clazz) {
		return getValue(name).getObject(clazz);
	}

	@Override
	public Class<?> getClassType(String name) {
		return getValue(name).getClassType();
	}
	
	@Override
	public Class<?> getClassType(String name, Class<?> defaultValue) {
		Class<?> value = getValue(name).getClassType();
		return null != value ? value : defaultValue;
	}
	
	@Override
	public Date getDate(String name, Object arg) {
		return getValue(name).getDate(arg);
	}

	@Override
	public Date getDate(String name, Object arg, Date defaultValue) {
		Date value = getValue(name).getDate(arg);
		return null != value ? value : defaultValue;
	}
	
	@Override
	public Date notNullDate(String name, Object arg, String message) {
		Date value = getValue(name).getDate(arg);
		Assert.notNull(value, message);
		return value;
	}
}
